package com.csnj.zip;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;


/**
 * @author Sam.Lee
 * @version Created：Dec 2, 2011 11:07:47 AM
 * 在src目录下生成一个zip文件，这个zip文件中同时又包含一个zip文件
 */
public class ZipOutputStreamTest01 {
	public static void main(String[] args) throws Exception {
		ZipOutputStream zout = new ZipOutputStream(new FileOutputStream(
				"src/sam.zip"));
		DataOutputStream dos = new DataOutputStream(zout);
		zout.putNextEntry(new ZipEntry("file01.txt"));
		dos.write("file01...".getBytes());

		zout.putNextEntry(new ZipEntry("file02.zip"));

		ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
		ZipOutputStream zout1 = new ZipOutputStream(arrayOutputStream);
		zout1.putNextEntry(new ZipEntry("zip1.txt"));
		DataOutputStream dos1 = new DataOutputStream(zout1);
		dos1.write("zip1...".getBytes());

		zout1.putNextEntry(new ZipEntry("zip2.txt"));
		dos1.write("zip2...".getBytes());
		// 需close，以生成zip文件，此zip文件现存于内存中
		dos1.close();
		zout1.close();

		// 将内存中zip文件数据“落地”
		dos.write(arrayOutputStream.toByteArray());

		arrayOutputStream.close();
		dos.close();
		zout.close();
		
	}
}
